using System.Diagnostics;
using DimensionsHelper.LanguageService.Symbols;
using DimensionsHelper.LanguageService.Syntax;

namespace DimensionsHelper.LanguageService;

public class AnalysisCallExpression : AnalysisExpression
{

    internal AnalysisCallExpression(CallExpression syntaxNode, AnalysisNode? parent) : base(AnalysisBoundKinds.Call,
        syntaxNode, parent)
    {
        Syntax = syntaxNode;
    }
    
    private readonly List<AnalysisExpression> _arguments = [];

    public AnalysisExpression? Callee { get; private set; }
    
    public override CallExpression Syntax { get; }

    public override int ChildrenCount => _arguments.Count + (Callee == null ? 0 : 1);


    public AnalysisExpression GetArgumentAt(int index)
    {
        return _arguments[index];
    }


    internal void SetCallee(AnalysisExpression callee)
    {
        Callee = callee;
    }


    public AnalysisExpression GetCallee()
    {
        Debug.Assert(Callee != null, "CallExpression does not have a callee.");
        return Callee;
    }


    public override IEnumerable<AnalysisNode> GetChildren()
    {
        return [GetCallee(), .._arguments];
    }

    internal override void AddChild(AnalysisNode child)
    {
        Debug.Assert(child.IsExpression);
        child.Parent = this;
        _arguments.Add((AnalysisExpression)child);
    }
}

public static partial class AnalysisNodeFactory
{
    /// <summary>
    /// 创建一个函数调用表达式
    /// </summary>
    /// <param name="syntaxNode">原始语法节点</param>
    /// <param name="parent">父节点，可以忽略</param>
    /// <param name="withSymbol">语法节点所在的With语句上下文类型符号</param>
    public static AnalysisCallExpression CallExpression(CallExpression syntaxNode, AnalysisNode? parent,
        ITypeSymbol? withSymbol)
    {
        return new AnalysisCallExpression(syntaxNode, parent).WithContainingWithSymbol(withSymbol);
    }
}